<?php

namespace plugin\voice\app\controller;

use Webman\Medoo\Medoo as db;
use support\Request;
use support\Response;

class SettingController extends Base
{
    protected $noNeedLogin = ['index'];
    
    public function index(){
        $setting                = db::get('voice_settings',['site_name','vad_eos','run_words']);
        if($setting['run_words']){
            $setting['run_words'] = explode(PHP_EOL,$setting['run_words']);
        }
        
        $modelType               = [];
        $models                  = db::select('voice_models',['id','name','field'],['status' => 1]);
        foreach ($models as $model) {
            $modelType[$model['field']] = $model['name'];
        }
        
        $newUserInfo             = [];
        $plans                   = db::select('voice_plans','*',['status' => 1]);
        foreach ($plans as &$plan) {
            $userModels          = [];
            $plan['average_pay'] = round($plan['price']/$plan['days'],2);
            foreach ($plan as $key => $value) {
                if (array_key_exists($key, $modelType)) {
                    if($value){
                        $userModels[] = ['name' => $modelType[$key],'value' => $value];
                    }
                } else {
                    $newUserInfo[$key] = $value; // 如果不在映射关系中，则保持原样
                }
            }
            $plan['models'] = $userModels;
        }
       
        
        $setting['debug']        = 0;
        $setting['xunfei']       = '/app/voice/audio/xunfeiIatAuth';
        $setting['sendApi']      = '/app/voice/chat';
        $setting['plans']        = $plans;
        $setting['models']       = $models;
        $setting['version']      = config('plugin.ai.app.version')?:config('plugin.gpt.app.version');
        return json(['code' => 0, 'msg' => 'ok', 'data' => $setting]);
    }
    
}
